package com.icss.clothes.cotroller;

import com.icss.clothes.entity.Address;
import com.icss.clothes.entity.User;
import com.icss.clothes.service.imp.UserService;
import com.icss.clothes.util.MD5Util;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 跳转到登录的控制器
     * @return
     */
    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }
    //用于实现用户登录的控制器方法
    @RequestMapping("/toSingIn")
    public ModelAndView toSingIn(String user_name, String upwd, HttpSession session){
        User user=userService.loginByUname(user_name);
        ModelAndView modelAndView=new ModelAndView();
        if (user!=null&& MD5Util.verifySaltPassword(upwd,user.getUpwd())){
            session.setAttribute("loginUser",user);
            modelAndView.setViewName("index");
        }else {
            modelAndView.addObject("msg","账户名或密码错误");
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    /**
     * 跳转到注册的控制器
     * @return
     */
    @RequestMapping("/loginup")
    public String toLoginUp(){
        return "loginup";
    }
    //用于实现用户注册的控制器方法
    @RequestMapping("/toRegister")
    public ModelAndView toRegister(User user, String Nupwd){
        String saltMD5Pwd= MD5Util.generateSaltPassword(user.getUpwd());
        user.setUpwd(saltMD5Pwd);
        ModelAndView modelAndView=new ModelAndView();
        if (MD5Util.verifySaltPassword(Nupwd,user.getUpwd())){
            boolean fang=userService.register(user);
            if (fang){
                modelAndView.setViewName("login");
            }else {
                modelAndView.addObject("abc","账户名已被占用");
                modelAndView.setViewName("loginup");
            }
        }else {
            modelAndView.addObject("acc","两次密码不一致");
            modelAndView.setViewName("loginup");
        }
        return modelAndView;
    }
    //实现用户退出的控制器方法
    @RequestMapping("/logout")
    public String toLogout(HttpSession session){
        //让会话失效
        session.invalidate();
        //跳转到登录页面
        return "login";
    }
    //用于实现跳转到个人信息页面的控制器方法
    @RequestMapping("/my")
    public String toMy(){
        return "my";
    }
    //用户实现修改个人信息的控制器方法
    @RequestMapping("/updateMy")
    public ModelAndView updateMy(User user,HttpSession session){

        ModelAndView modelAndView=new ModelAndView("my");
        //用于提示在修改用户名时存在的重名情况
        boolean flag= userService.updateUser(user);
        if(flag==true){
         if (user.getUpwd()!=null&&user.getUpwd()!=""){
            //让会话失效
            session.invalidate();
            //跳转到登录页面
            modelAndView.setViewName("login");
            modelAndView.addObject("msg","修改成功，请重新登录");
            }else {
            modelAndView.setViewName("index");
            modelAndView.addObject("msg","修改成功");
             }
        }else{
            modelAndView.addObject("msg","修改失败,用户名已经被占用");
        }
        return modelAndView;
    }
    //用于展现收货地址页面的控制器
    @RequestMapping("/toAddress")
    public ModelAndView toAddress(HttpSession session){
        ModelAndView modelAndView=new ModelAndView("address");
        User user=(User) session.getAttribute("loginUser");
        //需要一个能够通过指定的用户编号查询得到所有收货地址信息的业务逻辑
        List<Address> addressList=userService.findAllByUser_id(user.getUser_id());
        modelAndView.addObject("addressList",addressList);
        return modelAndView;
    }
}
